﻿using Unknown.Framework.Database;
using Unknown.Source.Sql.NormalView;

namespace Unknown.Source.OracleSql.NormalView
{
    /// <summary>
    /// 表SQL抽象类
    /// </summary>
    public abstract class CascadeTableOracleSql : ICascadeTableSql
    {
        /// <summary>
        /// 构建查询
        /// </summary>
        /// <param name="first">开始行</param>
        /// <param name="last">结束行</param>
        /// <returns>操作</returns>
        public virtual Operation BuildSelect(int first, int last)
        {
            CascadeTableOperationHandler operationHandler = new CascadeTableOperationHandler();
            operationHandler.First = first;
            operationHandler.Last = last;

            Operation result = new Operation();

            result.Sql = @"
select a.table_name as name,
       b.comments as remark,
       c.column_name as columnname,
       c.data_type as columndatatype,
       c.data_length as columnlength,
       c.data_precision as columnprecision,
       c.data_scale as columnscale,
       decode(c.nullable, 'Y', 'True', 'False') as columnnullable,
       d.comments as columnremark
  from user_tables a
  left join user_tab_comments b on a.table_name = b.table_name
  left join user_tab_columns c on a.table_name = c.table_name
  left join user_col_comments d on c.table_name = d.table_name
                               and c.column_name = d.column_name
 order by a.table_name, c.column_id
";

            result.OperationHandler = operationHandler;

            return result;
        }
    }
}
